package com.cloudcms.web.service.impl;

import com.alibaba.fastjson.JSON;
import com.cloudcms.web.entity.Types;
import com.cloudcms.web.service.TreeService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TreeServiceImpl implements TreeService {

    @Override
    public Types getParentTypes(String typeid, List<Types> list) {
        return null;
    }

    public List<Types> getLocation2(String typeid, List<Types> nodes,List<Types> result){
        if(result.size()>0&&"1".equals(result.get(result.size()-1).getId())){
            return  result;
        }else{
            for (int i=0,len=nodes.size();i<len;i++){
                if (typeid.equals(nodes.get(i).getId())){
                    result.add(nodes.get(i));
                    getLocation2(nodes.get(i).getParentid(),nodes,result);
                    break;
                }
            }
            return  result;
        }
    }
    @Override
    public List<Types> getLocation(String typeid,List<Types> nodes) {
        Map<String, List<Types>> children = nodes.stream().filter(
                node -> !"1".equals(node.getId())
        ).collect(
                Collectors.groupingBy(node -> node.getParentid())
        );
        List<Types> location = getLocation2(typeid,nodes,new ArrayList<Types>());
        Collections.reverse(location);
        location.remove(0);
        location.forEach(node -> node.setChildren(JSON.toJSONString(children.get(node.getId()))));

        return location;
    }
    @Override
    public List<Types> buildTree(List<Types> nodes,String parentid) {
        Map<String, List<Types>> children = nodes.stream().filter(
                node -> !"1".equals(node.getId())
        ).collect(
                Collectors.groupingBy(node -> node.getParentid())
        );
        nodes.forEach(node -> node.setChildren(children.get(node.getId())));
        //return nodes;
        return nodes.stream().filter(node -> parentid.equals(node.getId())).collect(Collectors.toList());
    }

    public static void main(String[] args){

        /*List<Types> param = new ArrayList<Types>();
        List<Types> result = new ArrayList<Types>();
        Types types = new Types();
        types.setId("1");
        param.add(types);

        Types types2 = new Types();
        types2.setId("2");
        types2.setParentid("1");
        param.add(types2);

        Types types3 = new Types();
        types3.setId("3");
        types3.setParentid("2");
        param.add(types3);

        Types types4 = new Types();
        types4.setId("4");
        types4.setParentid("2");
        param.add(types4);
        for (int i=0,len=param.size();i<len;i++){
            System.out.println(param.get(i).getId());
        }
        List<Types> result2 = getLocation2("6",param,result);
        for (int i=0,len=result2.size();i<len;i++){
            System.out.println(result2.get(i).getId());
        }*/
    }
}
